home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Libraries & Documentation / Add-ons / Graphic effects / Pixels.p < prev    next >
Text File  |  1995-11-10  |  14KB  |  506 lines

  1. { Plotting sets of pixels fast. }
  2. { All reasonable screen depths supported: 1, 4, 8, 16 and 32 bits. }
  3.  
  4. unit Pixels;
  5. interface
  6.     uses
  7. {$ifc UNDEFINED THINK_PASCAL}
  8.         Types, QuickDraw, Windows, Dialogs, Fonts, Memory, {}
  9.         SegLoad, Scrap, ToolUtils, {}
  10. {$ELSEC}
  11. {$SETC GENERATINGPOWERPC = false}
  12. {$endc}
  13.         SAT;
  14.  
  15.     type
  16.         Pixel = record
  17.                 position: Point;
  18.                 data1, data2, data3, data4: SignedByte;
  19.             end;
  20.         Pixels = array[0..32000] of Pixel;
  21.         PixelPtr = ^Pixels;
  22.  
  23.     procedure SATDrawPixels (pix: PixelPtr; var port: SATPort; value: Longint);
  24.     procedure SATCopyPixels (pix: PixelPtr; var src, dest: SATPort);
  25.     procedure SATDrawPixelsSafe (pix: PixelPtr; var port: SATPort; value: Longint);
  26.     procedure SATCopyPixelsSafe (pix: PixelPtr; var src, dest: SATPort);
  27.  
  28. implementation
  29.  
  30.     type
  31.         LongPtr = ^Longint;
  32.         IntPtr = ^Integer;
  33.     var
  34.         gBitTablesInitialized: Boolean;
  35.         bits, bitValues, masks: array[0..7] of Byte;
  36.  
  37.     type
  38.         SATRowListType = array[0..480] of Ptr; {skall vara 0..0}
  39.         SATRowListPtr = ^SATRowListType;
  40.  
  41.     procedure InitializeBitTables;
  42.         var
  43.             bit, i: integer;
  44.     begin
  45.         bit := 128;
  46.         for i := 0 to 7 do
  47.             begin
  48.                 bits[i] := bit;
  49. {bitValues[i] := BitAnd(bit, value);}
  50. {$PUSH}
  51. {$R-}
  52.                 masks[i] := BitNot(bit);
  53.                 bit := BSR(bit, 1);
  54. {$POP}
  55.             end;
  56.         gBitTablesInitialized := true;
  57.     end; {InitializeBitTables}
  58.  
  59.     procedure SATDrawPixels (pix: PixelPtr; var port: SATPort; value: Longint);
  60.         var
  61.             mmuMode: SignedByte;
  62.             depth, i, count: Integer;
  63.             byteVal: SignedByte;
  64.             intVal: Integer;
  65.             lp: LongPtr;
  66.             ip: IntPtr;
  67.             bp: Ptr;
  68.             byteValEven, byteValOdd: SignedByte;
  69.             phase: Integer;
  70.     begin
  71.         if pix = nil then
  72.             Exit(SATDrawPixels);
  73.         count := GetPtrSize(Ptr(pix)) div SizeOf(Pixel);
  74.  
  75.         if gSAT.colorFlag then
  76.             depth := port.device^^.gdPMap^^.pixelSize
  77.         else
  78.             depth := 0;
  79.  
  80. {if 24-bit, swap to 32-bit}
  81. {$IFC NOT GENERATINGPOWERPC }
  82.         if gSAT.mmuMode <> true32b then
  83.             begin
  84.                 mmuMode := true32b;
  85.                 pix := PixelPtr(StripAddress(Ptr(pix)));
  86.                 SwapMMUMode(mmuMode);
  87.             end;
  88. {$ENDC}
  89.  
  90.         case depth of
  91.             0, 1: 
  92.                 begin
  93.                     if not gBitTablesInitialized then
  94.                         InitializeBitTables;
  95.                     for i := 0 to 7 do
  96.                         begin
  97.                             bitValues[i] := BitAnd(bits[i], value);
  98.                         end;
  99.  
  100.                     for i := 0 to count - 1 do
  101.                         begin
  102.                             bp := Ptr(BSR(pix^[i].position.h, 3) + Longint(SATRowListPtr(port.rows)^[pix^[i].position.v]));
  103.                             phase := BitAnd(pix^[i].position.h, 3);
  104. {$PUSH}
  105. {$R-}
  106.                             bp^ := BitOr(BitAnd(bp^, masks[phase]), bitValues[phase])
  107. {$POP}
  108.                         end;
  109.  
  110.                 end;
  111.             2: 
  112.                 begin
  113. {Pointless screen depth - ignored!}
  114.                 end;
  115.             4: 
  116.                 begin
  117. {$PUSH}
  118. {$R-}
  119.                     byteVal := value;
  120.                     byteValEven := BitAnd(byteVal, $f0);
  121.                     byteValOdd := BitAnd(byteVal, $0f);
  122. {$POP}
  123.                     for i := 0 to count - 1 do
  124.                         begin
  125.                             bp := Ptr(BSR(pix^[i].position.h, 1) + Longint(SATRowListPtr(port.rows)^[pix^[i].position.v]));
  126.                             if BitAnd(pix^[i].position.h, 1) = 0 then
  127. {$PUSH}
  128. {$R-}
  129.                                 bp^ := BitOr(BitAnd(bp^, $0f), byteValEven)
  130.                             else
  131.                                 bp^ := BitOr(BitAnd(bp^, $f0), byteValOdd);
  132. {$POP}
  133.                         end;
  134.                 end;
  135.             8: 
  136.                 begin
  137.                     byteVal := value;
  138.                     for i := 0 to count - 1 do
  139.                         begin
  140.                             bp := Ptr(pix^[i].position.h + Longint(SATRowListPtr(port.rows)^[pix^[i].position.v]));
  141.                             bp^ := byteVal;
  142.                         end;
  143.                 end;
  144.             16: 
  145.                 begin
  146.                     intVal := value;
  147.                     for i := 0 to count - 1 do
  148.                         begin
  149.                             ip := IntPtr(BSL(pix^[i].position.h, 1) + Longint(SATRowListPtr(port.rows)^[pix^[i].position.v]));
  150.                             ip^ := intVal;
  151.                         end;
  152.                 end;
  153.             32: 
  154.                 begin
  155.                     for i := 0 to count - 1 do
  156.                         begin
  157.                             lp := LongPtr(BSL(pix^[i].position.h, 2) + Longint(SATRowListPtr(port.rows)^[pix^[i].position.v]));
  158.                             lp^ := value;
  159.                         end;
  160.                 end;
  161.             otherwise
  162.         end;
  163.  
  164. {swap back}
  165. {$IFC NOT GENERATINGPOWERPC }
  166.         if gSAT.mmuMode <> true32b then
  167.             begin
  168.                 SwapMMUMode(mmuMode);
  169.             end;
  170. {$ENDC}
  171.     end; {SATDrawPixels}
  172.  
  173.     procedure SATCopyPixels (pix: PixelPtr; var src, dest: SATPort);
  174.         var
  175.             mmuMode: SignedByte;
  176.             depth, i, count: Integer;
  177.             lp, ldp: LongPtr;
  178.             ip, idp: IntPtr;
  179.             bp, bdp: Ptr;
  180.     begin
  181.         if pix = nil then
  182.             Exit(SATCopyPixels);
  183.         count := GetPtrSize(Ptr(pix)) div SizeOf(Pixel);
  184.  
  185.         if gSAT.colorFlag then
  186.             depth := dest.device^^.gdPMap^^.pixelSize
  187.         else
  188.             depth := 0;
  189.  
  190. {if 24-bit, swap to 32-bit}
  191. {$IFC NOT GENERATINGPOWERPC }
  192.         if gSAT.mmuMode <> true32b then
  193.             begin
  194.                 mmuMode := true32b;
  195.                 pix := PixelPtr(StripAddress(Ptr(pix)));
  196.                 SwapMMUMode(mmuMode);
  197.             end;
  198. {$ENDC}
  199.  
  200.         case depth of
  201.             0, 1: 
  202.                 begin
  203.                     if not gBitTablesInitialized then
  204.                         InitializeBitTables;
  205.                     for i := 0 to count - 1 do
  206.                         begin
  207.                             bp := Ptr(BSR(pix^[i].position.h, 3) + Longint(SATRowListPtr(src.rows)^[pix^[i].position.v]));
  208.                             bdp := Ptr(BSR(pix^[i].position.h, 3) + Longint(SATRowListPtr(dest.rows)^[pix^[i].position.v]));
  209.                             bdp^ := bp^;
  210.                         end;
  211.  
  212.                 end;
  213.             2: 
  214.                 begin
  215. {Pointless screen depth - ignored!}
  216.                 end;
  217.             4: 
  218.                 begin
  219.                     for i := 0 to count - 1 do
  220.                         begin
  221.                             bp := Ptr(BSR(pix^[i].position.h, 1) + Longint(SATRowListPtr(src.rows)^[pix^[i].position.v]));
  222.                             bdp := Ptr(BSR(pix^[i].position.h, 1) + Longint(SATRowListPtr(dest.rows)^[pix^[i].position.v]));
  223.                             bdp^ := bp^;
  224.                         end;
  225.                 end;
  226.             8: 
  227.                 begin
  228.                     for i := 0 to count - 1 do
  229.                         begin
  230.                             bp := Ptr(pix^[i].position.h + Longint(SATRowListPtr(src.rows)^[pix^[i].position.v]));
  231.                             bdp := Ptr(pix^[i].position.h + Longint(SATRowListPtr(dest.rows)^[pix^[i].position.v]));
  232.                             bdp^ := bp^;
  233.                         end;
  234.                 end;
  235.             16: 
  236.                 begin
  237.                     for i := 0 to count - 1 do
  238.                         begin
  239.                             ip := IntPtr(BSL(pix^[i].position.h, 1) + Longint(SATRowListPtr(src.rows)^[pix^[i].position.v]));
  240.                             idp := IntPtr(BSL(pix^[i].position.h, 1) + Longint(SATRowListPtr(dest.rows)^[pix^[i].position.v]));
  241.                             idp^ := ip^;
  242.                         end;
  243.                 end;
  244.             32: 
  245.                 begin
  246.                     for i := 0 to count - 1 do
  247.                         begin
  248.                             lp := LongPtr(BSL(pix^[i].position.h, 2) + Longint(SATRowListPtr(src.rows)^[pix^[i].position.v]));
  249.                             ldp := LongPtr(BSL(pix^[i].position.h, 2) + Longint(SATRowListPtr(dest.rows)^[pix^[i].position.v]));
  250.                             ldp^ := lp^;
  251.                         end;
  252.                 end;
  253.             otherwise
  254.         end;
  255.  
  256. {swap back}
  257. {$IFC NOT GENERATINGPOWERPC }
  258.         if gSAT.mmuMode <> true32b then
  259.             begin
  260.                 SwapMMUMode(mmuMode);
  261.             end;
  262. {$ENDC}
  263.     end; {SATCopyPixels}
  264.  
  265.  
  266.  
  267.     procedure SATDrawPixelsSafe (pix: PixelPtr; var port: SATPort; value: Longint);
  268.         var
  269.             mmuMode: SignedByte;
  270.             depth, i, count: Integer;
  271.             byteVal: SignedByte;
  272.             intVal: Integer;
  273.             lp: LongPtr;
  274.             ip: IntPtr;
  275.             bp: Ptr;
  276.             byteValEven, byteValOdd: SignedByte;
  277.             phase: Integer;
  278.     begin
  279.         if pix = nil then
  280.             Exit(SATDrawPixelsSafe);
  281.         count := GetPtrSize(Ptr(pix)) div SizeOf(Pixel);
  282.  
  283.         if gSAT.colorFlag then
  284.             depth := port.device^^.gdPMap^^.pixelSize
  285.         else
  286.             depth := 0;
  287.  
  288. {if 24-bit, swap to 32-bit}
  289. {$IFC NOT GENERATINGPOWERPC }
  290.         if gSAT.mmuMode <> true32b then
  291.             begin
  292.                 mmuMode := true32b;
  293.                 pix := PixelPtr(StripAddress(Ptr(pix)));
  294.                 SwapMMUMode(mmuMode);
  295.             end;
  296. {$ENDC}
  297.  
  298.         case depth of
  299.             0, 1: 
  300.                 begin
  301.                     if not gBitTablesInitialized then
  302.                         InitializeBitTables;
  303.                     for i := 0 to 7 do
  304.                         begin
  305.                             bitValues[i] := BitAnd(bits[i], value);
  306.                         end;
  307.  
  308.                     for i := 0 to count - 1 do
  309.                         if pix^[i].position.h >= port.bounds.left then
  310.                             if pix^[i].position.v >= port.bounds.top then
  311.                                 if pix^[i].position.h < port.bounds.right then
  312.                                     if pix^[i].position.h < port.bounds.bottom then
  313.                                         begin
  314.                                             bp := Ptr(BSR(pix^[i].position.h, 3) + Longint(SATRowListPtr(port.rows)^[pix^[i].position.v]));
  315.                                             phase := BitAnd(pix^[i].position.h, 3);
  316.                                             bp^ := BitOr(BitAnd(bp^, masks[phase]), bitValues[phase])
  317.                                         end;
  318.  
  319.                 end;
  320.             2: 
  321.                 begin
  322. {Pointless screen depth - ignored!}
  323.                 end;
  324.             4: 
  325.                 begin
  326.                     byteVal := value;
  327.                     byteValEven := BitAnd(byteVal, $f0);
  328.                     byteValOdd := BitAnd(byteVal, $0f);
  329.                     for i := 0 to count - 1 do
  330.                         if pix^[i].position.h >= port.bounds.left then
  331.                             if pix^[i].position.v >= port.bounds.top then
  332.                                 if pix^[i].position.h < port.bounds.right then
  333.                                     if pix^[i].position.h < port.bounds.bottom then
  334.                                         begin
  335.                                             bp := Ptr(BSR(pix^[i].position.h, 1) + Longint(SATRowListPtr(port.rows)^[pix^[i].position.v]));
  336.                                             if BitAnd(pix^[i].position.h, 1) = 0 then
  337.                                                 bp^ := BitOr(BitAnd(bp^, $0f), byteValEven)
  338.                                             else
  339.                                                 bp^ := BitOr(BitAnd(bp^, $f0), byteValOdd);
  340.                                         end;
  341.                 end;
  342.             8: 
  343.                 begin
  344.                     byteVal := value;
  345.                     for i := 0 to count - 1 do
  346.                         if pix^[i].position.h >= port.bounds.left then
  347.                             if pix^[i].position.v >= port.bounds.top then
  348.                                 if pix^[i].position.h < port.bounds.right then
  349.                                     if pix^[i].position.h < port.bounds.bottom then
  350.                                         begin
  351.                                             bp := Ptr(pix^[i].position.h + Longint(SATRowListPtr(port.rows)^[pix^[i].position.v]));
  352.                                             bp^ := byteVal;
  353.                                         end;
  354.                 end;
  355.             16: 
  356.                 begin
  357.                     intVal := value;
  358.                     for i := 0 to count - 1 do
  359.                         if pix^[i].position.h >= port.bounds.left then
  360.                             if pix^[i].position.v >= port.bounds.top then
  361.                                 if pix^[i].position.h < port.bounds.right then
  362.                                     if pix^[i].position.h < port.bounds.bottom then
  363.                                         begin
  364.                                             ip := IntPtr(BSL(pix^[i].position.h, 1) + Longint(SATRowListPtr(port.rows)^[pix^[i].position.v]));
  365.                                             ip^ := intVal;
  366.                                         end;
  367.                 end;
  368.             32: 
  369.                 begin
  370.                     for i := 0 to count - 1 do
  371.                         if pix^[i].position.h >= port.bounds.left then
  372.                             if pix^[i].position.v >= port.bounds.top then
  373.                                 if pix^[i].position.h < port.bounds.right then
  374.                                     if pix^[i].position.h < port.bounds.bottom then
  375.                                         begin
  376.                                             lp := LongPtr(BSL(pix^[i].position.h, 2) + Longint(SATRowListPtr(port.rows)^[pix^[i].position.v]));
  377.                                             lp^ := value;
  378.                                         end;
  379.                 end;
  380.             otherwise
  381.         end;
  382.  
  383.  
  384. {swap back}
  385. {$IFC NOT GENERATINGPOWERPC }
  386.         if gSAT.mmuMode <> true32b then
  387.             begin
  388.                 SwapMMUMode(mmuMode);
  389.             end;
  390. {$ENDC}
  391.     end; {SATDrawPixelsSafe}
  392.  
  393.     procedure SATCopyPixelsSafe (pix: PixelPtr; var src, dest: SATPort);
  394.         var
  395.             mmuMode: SignedByte;
  396.             depth, i, count: Integer;
  397.             lp, ldp: LongPtr;
  398.             ip, idp: IntPtr;
  399.             bp, bdp: Ptr;
  400.     begin
  401.         if pix = nil then
  402.             Exit(SATCopyPixelsSafe);
  403.         count := GetPtrSize(Ptr(pix)) div SizeOf(Pixel);
  404.  
  405.         if gSAT.colorFlag then
  406.             depth := dest.device^^.gdPMap^^.pixelSize
  407.         else
  408.             depth := 0;
  409.  
  410. {if 24-bit, swap to 32-bit}
  411. {$IFC NOT GENERATINGPOWERPC }
  412.         if gSAT.mmuMode <> true32b then
  413.             begin
  414.                 mmuMode := true32b;
  415.                 pix := PixelPtr(StripAddress(Ptr(pix)));
  416.                 SwapMMUMode(mmuMode);
  417.             end;
  418. {$ENDC}
  419.  
  420.         case depth of
  421.             0, 1: 
  422.                 begin
  423.                     if not gBitTablesInitialized then
  424.                         InitializeBitTables;
  425.  
  426.                     for i := 0 to count - 1 do
  427.                         if pix^[i].position.h >= dest.bounds.left then
  428.                             if pix^[i].position.v >= dest.bounds.top then
  429.                                 if pix^[i].position.h < dest.bounds.right then
  430.                                     if pix^[i].position.h < dest.bounds.bottom then
  431.                                         begin
  432.                                             bp := Ptr(BSR(pix^[i].position.h, 3) + Longint(SATRowListPtr(src.rows)^[pix^[i].position.v]));
  433.                                             bdp := Ptr(BSR(pix^[i].position.h, 3) + Longint(SATRowListPtr(dest.rows)^[pix^[i].position.v]));
  434.                                             bdp^ := bp^;
  435.                                         end;
  436.  
  437.                 end;
  438.             2: 
  439.                 begin
  440. {Pointless screen depth - ignored!}
  441.                 end;
  442.             4: 
  443.                 begin
  444.                     for i := 0 to count - 1 do
  445.                         if pix^[i].position.h >= dest.bounds.left then
  446.                             if pix^[i].position.v >= dest.bounds.top then
  447.                                 if pix^[i].position.h < dest.bounds.right then
  448.                                     if pix^[i].position.h < dest.bounds.bottom then
  449.                                         begin
  450.                                             bp := Ptr(BSR(pix^[i].position.h, 1) + Longint(SATRowListPtr(src.rows)^[pix^[i].position.v]));
  451.                                             bdp := Ptr(BSR(pix^[i].position.h, 1) + Longint(SATRowListPtr(dest.rows)^[pix^[i].position.v]));
  452.                                             bdp^ := bp^;
  453.                                         end;
  454.                 end;
  455.             8: 
  456.                 begin
  457.                     for i := 0 to count - 1 do
  458.                         if pix^[i].position.h >= dest.bounds.left then
  459.                             if pix^[i].position.v >= dest.bounds.top then
  460.                                 if pix^[i].position.h < dest.bounds.right then
  461.                                     if pix^[i].position.h < dest.bounds.bottom then
  462.                                         begin
  463.                                             bp := Ptr(pix^[i].position.h + Longint(SATRowListPtr(src.rows)^[pix^[i].position.v]));
  464.                                             bdp := Ptr(pix^[i].position.h + Longint(SATRowListPtr(dest.rows)^[pix^[i].position.v]));
  465.                                             bdp^ := bp^;
  466.                                         end;
  467.                 end;
  468.             16: 
  469.                 begin
  470.                     for i := 0 to count - 1 do
  471.                         if pix^[i].position.h >= dest.bounds.left then
  472.                             if pix^[i].position.v >= dest.bounds.top then
  473.                                 if pix^[i].position.h < dest.bounds.right then
  474.                                     if pix^[i].position.h < dest.bounds.bottom then
  475.                                         begin
  476.                                             ip := IntPtr(BSL(pix^[i].position.h, 1) + Longint(SATRowListPtr(src.rows)^[pix^[i].position.v]));
  477.                                             idp := IntPtr(BSL(pix^[i].position.h, 1) + Longint(SATRowListPtr(dest.rows)^[pix^[i].position.v]));
  478.                                             idp^ := ip^;
  479.                                         end;
  480.                 end;
  481.             32: 
  482.                 begin
  483.                     for i := 0 to count - 1 do
  484.                         if pix^[i].position.h >= dest.bounds.left then
  485.                             if pix^[i].position.v >= dest.bounds.top then
  486.                                 if pix^[i].position.h < dest.bounds.right then
  487.                                     if pix^[i].position.h < dest.bounds.bottom then
  488.                                         begin
  489.                                             lp := LongPtr(BSL(pix^[i].position.h, 2) + Longint(SATRowListPtr(src.rows)^[pix^[i].position.v]));
  490.                                             ldp := LongPtr(BSL(pix^[i].position.h, 2) + Longint(SATRowListPtr(dest.rows)^[pix^[i].position.v]));
  491.                                             ldp^ := lp^;
  492.                                         end;
  493.                 end;
  494.             otherwise
  495.         end;
  496.  
  497. {swap back}
  498. {$IFC NOT GENERATINGPOWERPC }
  499.         if gSAT.mmuMode <> true32b then
  500.             begin
  501.                 SwapMMUMode(mmuMode);
  502.             end;
  503. {$ENDC}
  504.     end; {SATCopyPixelsSafe}
  505.  
  506. end.